package com.sc.service.serviceImpl;

import com.sc.common.pojo.EasyUiTreeNode;
import com.sc.mapper.TbItemCatMapper;
import com.sc.pojo.TbItem;
import com.sc.pojo.TbItemCat;
import com.sc.pojo.TbItemCatExample;
import com.sc.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sc on 2017/10/10.
 */
@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private TbItemCatMapper itemCat;

    public List<EasyUiTreeNode> getItemCat(long parentId) {
        TbItemCatExample example =  new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbItemCat> list =  itemCat.selectByExample(example);
        List<EasyUiTreeNode> easyUiTreeNodeList = assambleTreeNode(list);
        return easyUiTreeNodeList;

    }


    public List<EasyUiTreeNode> assambleTreeNode(List<TbItemCat> itemCatList){
        List<EasyUiTreeNode> list =  new ArrayList<EasyUiTreeNode>();
        for (TbItemCat item : itemCatList){
            EasyUiTreeNode node  = new EasyUiTreeNode();
            node.setId(item.getId());
            node.setText(item.getName());
            node.setState(item.getIsParent()?"closed":"open");
            list.add(node);
        }
        return list;
    }


}
